#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "vector.h"
#include <limits.h>

// 处理索引，支持负索引
static int _VectorProcessIndex(const Vector* vector, int index) {
    if (!vector) {
        return -1;
    }
    
    // 处理负索引
    if (index < 0) {
        index += vector->size;
        if (index < 0) {
            return -1;  // 索引越界
        }
    } else if ((size_t)index >= vector->size) {
        return -1;  // 索引越界
    }
    
    return index;
}

// 获取类型大小
size_t VectorGetTypeSize(VectorType type) {
    switch (type) {
        case VECTOR_TYPE_INT: return sizeof(int);
        case VECTOR_TYPE_FLOAT: return sizeof(float);
        case VECTOR_TYPE_DOUBLE: return sizeof(double);
        case VECTOR_TYPE_CHAR: return sizeof(char);
        case VECTOR_TYPE_SHORT: return sizeof(short);
        case VECTOR_TYPE_LONG: return sizeof(long);
        case VECTOR_TYPE_LONGLONG: return sizeof(long long);
        case VECTOR_TYPE_UINT: return sizeof(unsigned int);
        case VECTOR_TYPE_UCHAR: return sizeof(unsigned char);
        case VECTOR_TYPE_USHORT: return sizeof(unsigned short);
        case VECTOR_TYPE_ULONG: return sizeof(unsigned long);
        case VECTOR_TYPE_ULONGLONG: return sizeof(unsigned long long);
        case VECTOR_TYPE_BOOL: return sizeof(_Bool);
        default: return 0;
    }
}

// 创建新的Vector
Vector* VectorNew(VectorType type, size_t initial_capacity) {
    Vector* vector = (Vector*)malloc(sizeof(Vector));
    if (vector == NULL) {
        fprintf(stderr, "VectorNew: malloc failed\n");
        return NULL;
    }
    
    vector->type = type;
    vector->size = 0;
    vector->element_size = VectorGetTypeSize(type);
    vector->capacity = (initial_capacity > 0) ? initial_capacity : 10; // 默认容量为10
    
    vector->data = malloc(vector->capacity * vector->element_size);
    if (vector->data == NULL) {
        fprintf(stderr, "VectorNew: data malloc failed\n");
        free(vector);
        return NULL;
    }
    
    return vector;
}

// 释放Vector内存
void VectorFree(Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorFree: vector is NULL\n");
        return;
    }
    
    free(vector->data);
    free(vector);
}

// 获取Vector大小
size_t VectorSize(const Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorSize: vector is NULL\n");
        return 0;
    }
    return vector->size;
}

// 获取Vector容量
size_t VectorCapacity(const Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorCapacity: vector is NULL\n");
        return 0;
    }
    return vector->capacity;
}

// 清空Vector
void VectorClear(Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorClear: vector is NULL\n");
        return;
    }
    vector->size = 0;
}

// 确保Vector至少有min_capacity的容量
void VectorEnsureCapacity(Vector* vector, size_t min_capacity) {
    if (vector == NULL) {
        fprintf(stderr, "VectorEnsureCapacity: vector is NULL\n");
        return;
    }
    
    if (min_capacity > vector->capacity) {
        size_t new_capacity = vector->capacity * 2;
        if (new_capacity < min_capacity) {
            new_capacity = min_capacity;
        }
        
        void* new_data = realloc(vector->data, new_capacity * vector->element_size);
        if (new_data == NULL) {
            fprintf(stderr, "VectorEnsureCapacity: realloc failed\n");
            exit(EXIT_FAILURE);
        }
        
        vector->data = new_data;
        vector->capacity = new_capacity;
    }
}

// 调整Vector容量
void VectorReserve(Vector* vector, size_t new_capacity) {
    if (vector == NULL) {
        fprintf(stderr, "VectorReserve: vector is NULL\n");
        return;
    }
    
    if (new_capacity > vector->capacity) {
        void* new_data = realloc(vector->data, new_capacity * vector->element_size);
        if (new_data == NULL) {
            fprintf(stderr, "VectorReserve: realloc failed\n");
            exit(EXIT_FAILURE);
        }
        
        vector->data = new_data;
        vector->capacity = new_capacity;
    }
}

// 内部辅助函数：获取元素指针
void* VectorGetElementPtr(const Vector* vector, int index) {
    if (vector == NULL) {
        fprintf(stderr, "VectorGetElementPtr: vector is NULL\n");
        return NULL;
    }
    
    // 修复有符号和无符号整数比较问题
    if (index < 0) {
        if (-index > (int)vector->size) {
            fprintf(stderr, "VectorGetElementPtr: index out of range\n");
            return NULL;
        }
        index += vector->size;
    } else {
        if ((size_t)index >= vector->size) {
            fprintf(stderr, "VectorGetElementPtr: index out of range\n");
            return NULL;
        }
    }
    
    return (char*)vector->data + index * vector->element_size;
}

// 添加元素的通用函数
void VectorAddGeneric(Vector* vector, const void* value) {
    if (vector == NULL) {
        fprintf(stderr, "VectorAddGeneric: vector is NULL\n");
        return;
    }
    
    VectorEnsureCapacity(vector, vector->size + 1);
    memcpy((char*)vector->data + vector->size * vector->element_size, value, vector->element_size);
    vector->size++;
}

// Add函数的具体类型实现
void VectorAddInt(Vector* vector, int value) { VectorAddGeneric(vector, &value); }
void VectorAddFloat(Vector* vector, float value) { VectorAddGeneric(vector, &value); }
void VectorAddDouble(Vector* vector, double value) { VectorAddGeneric(vector, &value); }
void VectorAddChar(Vector* vector, char value) { VectorAddGeneric(vector, &value); }
void VectorAddShort(Vector* vector, short value) { VectorAddGeneric(vector, &value); }
void VectorAddLong(Vector* vector, long value) { VectorAddGeneric(vector, &value); }
void VectorAddLongLong(Vector* vector, long long value) { VectorAddGeneric(vector, &value); }
void VectorAddUInt(Vector* vector, unsigned int value) { VectorAddGeneric(vector, &value); }
void VectorAddUChar(Vector* vector, unsigned char value) { VectorAddGeneric(vector, &value); }
void VectorAddUShort(Vector* vector, unsigned short value) { VectorAddGeneric(vector, &value); }
void VectorAddULong(Vector* vector, unsigned long value) { VectorAddGeneric(vector, &value); }
void VectorAddULongLong(Vector* vector, unsigned long long value) { VectorAddGeneric(vector, &value); }
void VectorAddBool(Vector* vector, int value) { VectorAddGeneric(vector, &value); }

// 通用获取元素函数，根据vector类型返回对应元素的指针
void* VectorGetByType(Vector* vector, int index) {
    if (!vector) {
        return NULL;
    }
    
    // 处理索引
    int actual_index = _VectorProcessIndex(vector, index);
    if (actual_index < 0) {
        return NULL;
    }
    
    // 计算元素的内存地址
    return (char*)vector->data + actual_index * vector->element_size;
}

// Get函数的具体类型实现
int VectorGetInt(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(int*)ptr : 0;
}

float VectorGetFloat(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(float*)ptr : 0.0f;
}

double VectorGetDouble(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(double*)ptr : 0.0;
}

char VectorGetChar(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(char*)ptr : 0;
}

short VectorGetShort(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(short*)ptr : 0;
}

long VectorGetLong(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(long*)ptr : 0L;
}

long long VectorGetLongLong(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(long long*)ptr : 0LL;
}

unsigned int VectorGetUInt(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(unsigned int*)ptr : 0U;
}

unsigned char VectorGetUChar(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(unsigned char*)ptr : 0U;
}

unsigned short VectorGetUShort(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(unsigned short*)ptr : 0U;
}

unsigned long VectorGetULong(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(unsigned long*)ptr : 0UL;
}

unsigned long long VectorGetULongLong(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(unsigned long long*)ptr : 0ULL;
}

int VectorGetBool(const Vector* vector, int index) {
    void* ptr = VectorGetElementPtr(vector, index);
    return ptr ? *(int*)ptr : 0;
}

// Set函数的具体类型实现
void VectorSetInt(Vector* vector, int index, int value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(int*)ptr = value;
    }
}

void VectorSetFloat(Vector* vector, int index, float value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(float*)ptr = value;
    }
}

void VectorSetDouble(Vector* vector, int index, double value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(double*)ptr = value;
    }
}

void VectorSetChar(Vector* vector, int index, char value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(char*)ptr = value;
    }
}

void VectorSetShort(Vector* vector, int index, short value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(short*)ptr = value;
    }
}

void VectorSetLong(Vector* vector, int index, long value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(long*)ptr = value;
    }
}

void VectorSetLongLong(Vector* vector, int index, long long value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(long long*)ptr = value;
    }
}

void VectorSetUInt(Vector* vector, int index, unsigned int value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(unsigned int*)ptr = value;
    }
}

void VectorSetUChar(Vector* vector, int index, unsigned char value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(unsigned char*)ptr = value;
    }
}

void VectorSetUShort(Vector* vector, int index, unsigned short value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(unsigned short*)ptr = value;
    }
}

void VectorSetULong(Vector* vector, int index, unsigned long value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(unsigned long*)ptr = value;
    }
}

void VectorSetULongLong(Vector* vector, int index, unsigned long long value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(unsigned long long*)ptr = value;
    }
}

void VectorSetBool(Vector* vector, int index, int value) {
    void* ptr = VectorGetElementPtr(vector, index);
    if (ptr) {
        *(int*)ptr = value;
    }
}

// 删除元素
void VectorDelete(Vector* vector, int index) {
    if (vector == NULL) {
        fprintf(stderr, "VectorDelete: vector is NULL\n");
        return;
    }
    
    // 获取有效的索引
    if (index < 0) {
        if (-index > (int)vector->size) {
            fprintf(stderr, "VectorDelete: index out of range\n");
            return;
        }
        index += vector->size;
    } else {
        if ((size_t)index >= vector->size) {
            fprintf(stderr, "VectorDelete: index out of range\n");
            return;
        }
    }
    
    // 移动元素覆盖被删除的元素
    size_t bytes_to_move = (vector->size - index - 1) * vector->element_size;
    if (bytes_to_move > 0) {
        memmove((char*)vector->data + index * vector->element_size,
                (char*)vector->data + (index + 1) * vector->element_size,
                bytes_to_move);
    }
    
    vector->size--;
    
    // 缩小容量（如果需要）
    if (vector->size < vector->capacity / 2 && vector->capacity > 10) {
        size_t new_capacity = vector->capacity / 2;
        void* new_data = realloc(vector->data, new_capacity * vector->element_size);
        if (new_data == NULL) {
            fprintf(stderr, "VectorDelete: realloc failed\n");
            exit(EXIT_FAILURE);
        }
        vector->data = new_data;
        vector->capacity = new_capacity;
    }
}

// 删除指定范围的元素
void VectorDeleteRange(Vector* vector, int start, int end) {
    if (vector == NULL) {
        fprintf(stderr, "VectorDeleteRange: vector is NULL\n");
        return;
    }
    
    // 处理start索引
    if (start < 0) {
        if (-start > (int)vector->size) {
            fprintf(stderr, "VectorDeleteRange: index out of range\n");
            return;
        }
        start += vector->size;
    } else {
        if ((size_t)start >= vector->size) {
            fprintf(stderr, "VectorDeleteRange: index out of range\n");
            return;
        }
    }
    
    // 处理end索引
    if (end < 0) {
        if (-end > (int)vector->size) {
            fprintf(stderr, "VectorDeleteRange: index out of range\n");
            return;
        }
        end += vector->size;
    } else {
        if ((size_t)end > vector->size) {
            fprintf(stderr, "VectorDeleteRange: index out of range\n");
            return;
        }
    }
    
    if (start >= end) {
        return;
    }
    
    size_t count = end - start;
    size_t bytes_to_move = (vector->size - end) * vector->element_size;
    
    if (bytes_to_move > 0) {
        memmove((char*)vector->data + start * vector->element_size,
                (char*)vector->data + end * vector->element_size,
                bytes_to_move);
    }
    
    vector->size -= count;
    
    // 缩小容量（如果需要）
    if (vector->size < vector->capacity / 2 && vector->capacity > 10) {
        size_t new_capacity = vector->capacity / 2;
        void* new_data = realloc(vector->data, new_capacity * vector->element_size);
        if (new_data == NULL) {
            fprintf(stderr, "VectorDeleteRange: realloc failed\n");
            exit(EXIT_FAILURE);
        }
        vector->data = new_data;
        vector->capacity = new_capacity;
    }
}

// 复制Vector
Vector* VectorCopy(const Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorCopy: vector is NULL\n");
        return NULL;
    }
    
    Vector* new_vector = VectorNew(vector->type, vector->capacity);
    if (new_vector == NULL) {
        return NULL;
    }
    
    memcpy(new_vector->data, vector->data, vector->size * vector->element_size);
    new_vector->size = vector->size;
    
    return new_vector;
}

// 从数组创建Vector
Vector* VectorFromArray(VectorType type, const void* array, size_t count) {
    if (array == NULL) {
        fprintf(stderr, "VectorFromArray: array is NULL\n");
        return NULL;
    }
    
    Vector* vector = VectorNew(type, count);
    if (vector == NULL) {
        return NULL;
    }
    
    memcpy(vector->data, array, count * vector->element_size);
    vector->size = count;
    
    return vector;
}

// 扩展Vector
void VectorExtend(Vector* vector, const Vector* extend_vector) {
    if (vector == NULL || extend_vector == NULL) {
        fprintf(stderr, "VectorExtend: vectors are NULL\n");
        return;
    }
    
    if (vector->type != extend_vector->type) {
        fprintf(stderr, "VectorExtend: vector types mismatch\n");
        return;
    }
    
    // 确保有足够的容量
    VectorEnsureCapacity(vector, vector->size + extend_vector->size);
    
    // 复制元素
    memcpy((char*)vector->data + vector->size * vector->element_size,
            extend_vector->data,
            extend_vector->size * vector->element_size);
    
    vector->size += extend_vector->size;
}

// 比较两个元素
int VectorCompareElements(const Vector* vector, const void* elem1, const void* elem2) {
    switch (vector->type) {
        case VECTOR_TYPE_INT: {
            int a = *(int*)elem1, b = *(int*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_FLOAT: {
            float a = *(float*)elem1, b = *(float*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_DOUBLE: {
            double a = *(double*)elem1, b = *(double*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_CHAR: {
            char a = *(char*)elem1, b = *(char*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_SHORT: {
            short a = *(short*)elem1, b = *(short*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_LONG: {
            long a = *(long*)elem1, b = *(long*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_LONGLONG: {
            long long a = *(long long*)elem1, b = *(long long*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_UINT: {
            unsigned int a = *(unsigned int*)elem1, b = *(unsigned int*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_UCHAR: {
            unsigned char a = *(unsigned char*)elem1, b = *(unsigned char*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_USHORT: {
            unsigned short a = *(unsigned short*)elem1, b = *(unsigned short*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_ULONG: {
            unsigned long a = *(unsigned long*)elem1, b = *(unsigned long*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_ULONGLONG: {
            unsigned long long a = *(unsigned long long*)elem1, b = *(unsigned long long*)elem2;
            return (a > b) - (a < b);
        }
        case VECTOR_TYPE_BOOL: {
            int a = *(int*)elem1, b = *(int*)elem2;
            return (a > b) - (a < b);
        }
        default:
            return 0;
    }
}

// 查找元素索引
int VectorIndex(const Vector* vector, const void* value) {
    if (vector == NULL || value == NULL) {
        fprintf(stderr, "VectorIndex: vector or value is NULL\n");
        return -1;
    }
    
    for (size_t i = 0; i < vector->size; i++) {
        void* elem = (char*)vector->data + i * vector->element_size;
        if (VectorCompareElements(vector, elem, value) == 0) {
            return (int)i;
        }
    }
    
    return -1;
}

// 检查元素是否存在
int VectorHas(const Vector* vector, const void* value) {
    return VectorIndex(vector, value) != -1;
}

// 计算元素出现次数
size_t VectorCountValue(const Vector* vector, const void* value) {
    if (vector == NULL || value == NULL) {
        fprintf(stderr, "VectorCountValue: vector or value is NULL\n");
        return 0;
    }
    
    size_t count = 0;
    for (size_t i = 0; i < vector->size; i++) {
        void* elem = (char*)vector->data + i * vector->element_size;
        if (VectorCompareElements(vector, elem, value) == 0) {
            count++;
        }
    }
    
    return count;
}

// 删除第一个匹配的元素
int VectorRemove(Vector* vector, const void* value) {
    int index = VectorIndex(vector, value);
    if (index != -1) {
        VectorDelete(vector, index);
        return 1;
    }
    return 0;
}

// 截取Vector
Vector* VectorSlice(const Vector* vector, int start, int end, int step) {
    if (vector == NULL) {
        fprintf(stderr, "VectorSlice: vector is NULL\n");
        return NULL;
    }
    
    if (step == 0) {
        fprintf(stderr, "VectorSlice: step is 0\n");
        return NULL;
    }
    
    // 处理start索引
    if (start < 0) {
        if (-start > (int)vector->size) {
            fprintf(stderr, "VectorSlice: index out of range\n");
            return NULL;
        }
        start += vector->size;
    } else {
        if ((size_t)start >= vector->size) {
            fprintf(stderr, "VectorSlice: index out of range\n");
            return NULL;
        }
    }
    
    // 处理end索引
    if (end < 0) {
        if (-end > (int)vector->size) {
            fprintf(stderr, "VectorSlice: index out of range\n");
            return NULL;
        }
        end += vector->size;
    } else {
        if ((size_t)end > vector->size) {
            fprintf(stderr, "VectorSlice: index out of range\n");
            return NULL;
        }
    }
    
    Vector* slice = VectorNew(vector->type, 0);
    if (slice == NULL) {
        return NULL;
    }
    
    // 根据step的正负来处理
    if (step > 0) {
        for (int i = start; i < end; i += step) {
            void* elem = (char*)vector->data + i * vector->element_size;
            VectorAddGeneric(slice, elem);
        }
    } else {
        for (int i = start; i > end; i += step) {
            void* elem = (char*)vector->data + i * vector->element_size;
            VectorAddGeneric(slice, elem);
        }
    }
    
    return slice;
}

// 获取最小值
void* VectorMinValue(const Vector* vector) {
    if (vector == NULL || vector->size == 0) {
        fprintf(stderr, "VectorMinValue: vector is NULL or empty\n");
        return NULL;
    }
    
    size_t min_index = 0;
    for (size_t i = 1; i < vector->size; i++) {
        void* curr = (char*)vector->data + i * vector->element_size;
        void* min = (char*)vector->data + min_index * vector->element_size;
        if (VectorCompareElements(vector, curr, min) < 0) {
            min_index = i;
        }
    }
    
    return (char*)vector->data + min_index * vector->element_size;
}

// 获取最大值
void* VectorMaxValue(const Vector* vector) {
    if (vector == NULL || vector->size == 0) {
        fprintf(stderr, "VectorMaxValue: vector is NULL or empty\n");
        return NULL;
    }
    
    size_t max_index = 0;
    for (size_t i = 1; i < vector->size; i++) {
        void* curr = (char*)vector->data + i * vector->element_size;
        void* max = (char*)vector->data + max_index * vector->element_size;
        if (VectorCompareElements(vector, curr, max) > 0) {
            max_index = i;
        }
    }
    
    return (char*)vector->data + max_index * vector->element_size;
}

// 交换两个元素
void VectorSwapElements(Vector* vector, size_t i, size_t j) {
    if (i != j) {
        char* data = (char*)vector->data;
        char* elem_i = data + i * vector->element_size;
        char* elem_j = data + j * vector->element_size;
        
        // 使用临时缓冲区交换元素
        char* temp = (char*)malloc(vector->element_size);
        if (temp != NULL) {
            memcpy(temp, elem_i, vector->element_size);
            memcpy(elem_i, elem_j, vector->element_size);
            memcpy(elem_j, temp, vector->element_size);
            free(temp);
        }
    }
}

// 反转Vector
void VectorReverse(Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorReverse: vector is NULL\n");
        return;
    }
    
    for (size_t i = 0; i < vector->size / 2; i++) {
        VectorSwapElements(vector, i, vector->size - 1 - i);
    }
}

// 快速排序辅助函数
void VectorQuickSort(Vector* vector, int low, int high) {
    if (low < high) {
        // 选择中间元素作为基准
        int mid = low + (high - low) / 2;
        VectorSwapElements(vector, mid, high);
        
        void* pivot = (char*)vector->data + high * vector->element_size;
        int i = low - 1;
        
        for (int j = low; j < high; j++) {
            void* elem = (char*)vector->data + j * vector->element_size;
            if (VectorCompareElements(vector, elem, pivot) <= 0) {
                i++;
                VectorSwapElements(vector, i, j);
            }
        }
        
        VectorSwapElements(vector, i + 1, high);
        int pivot_index = i + 1;
        
        VectorQuickSort(vector, low, pivot_index - 1);
        VectorQuickSort(vector, pivot_index + 1, high);
    }
}

// 排序Vector（升序）
void VectorSort(Vector* vector) {
    if (vector == NULL) {
        fprintf(stderr, "VectorSort: vector is NULL\n");
        return;
    }
    
    if (vector->size > 1) {
        VectorQuickSort(vector, 0, vector->size - 1);
    }
}

// 打印Vector内容（用于调试）
void VectorPrint(const Vector* vector) {
    if (vector == NULL) {
        printf("Vector is NULL\n");
        return;
    }
    
    printf("Vector(type=%d, size=%zu, capacity=%zu): [", 
           vector->type, vector->size, vector->capacity);
    
    for (size_t i = 0; i < vector->size; i++) {
        void* elem = (char*)vector->data + i * vector->element_size;
        
        switch (vector->type) {
            case VECTOR_TYPE_INT: printf("%d", *(int*)elem); break;
            case VECTOR_TYPE_FLOAT: printf("%f", *(float*)elem); break;
            case VECTOR_TYPE_DOUBLE: printf("%lf", *(double*)elem); break;
            case VECTOR_TYPE_CHAR: printf("'%c'", *(char*)elem); break;
            case VECTOR_TYPE_SHORT: printf("%hd", *(short*)elem); break;
            case VECTOR_TYPE_LONG: printf("%ld", *(long*)elem); break;
            case VECTOR_TYPE_LONGLONG: printf("%lld", *(long long*)elem); break;
            case VECTOR_TYPE_UINT: printf("%u", *(unsigned int*)elem); break;
            case VECTOR_TYPE_UCHAR: printf("%hhu", *(unsigned char*)elem); break;
            case VECTOR_TYPE_USHORT: printf("%hu", *(unsigned short*)elem); break;
            case VECTOR_TYPE_ULONG: printf("%lu", *(unsigned long*)elem); break;
            case VECTOR_TYPE_ULONGLONG: printf("%llu", *(unsigned long long*)elem); break;
            case VECTOR_TYPE_BOOL: printf("%d", *(int*)elem); break;
            default: printf("?");
        }
        
        if (i < vector->size - 1) {
            printf(", ");
        }
    }
    
    printf("]\n");
}